<?php

namespace App\Models;

use App\Service\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Renting extends Base
{
    use HasFactory;
    //声明用户表
    public $table = 'renting';
    protected $guarded=[];

    /**
     * @param $id
     * @return mixed
     * User: 张浩
     * Date: 2022/11/11
     * Time: 19:06
     * 根据用户id获取用户数据
     */
    public static function rentingFirst($id)
    {
        return Renting::where('id', $id)->first()->toArray();
    }

    /**
     * @param $post
     * @return array
     * User: 张浩
     * Date: 2022/11/11
     * Time: 18:46
     *
     */
    public static function wxForgetPasswor($post, $user)
    {
        $user['password'] = md5($post['password']);
        return Renting::where('phone', $post['phone'])->update($user);
    }

    /**
     * @param $post
     * @return array
     * @throws \Throwable
     * User: 张浩
     * Date: 2022/11/11
     * Time: 14:14
     * 用户注册时填写邀请码时注册
     */
    public static function wxPlaceCode($post)
    {
        //根据分销码获取上级用户信息
        $users = Renting::where('user_place_code', $post['place_code'])->first()->toArray();
        //上级用户的下级用户字段根据，转换为数组
        $users['place_junior'] = explode(',', $users['place_junior']);
        //生成用户名称
        $post['nickName'] = '手机用户' . time();
        //加密用户密码
        $post['password'] = md5($post['password']);
        $post['place_superior'] = $post['place_code'];
        unset($post['place_code']);
        try {
            \DB::beginTransaction();
            //注册新用户
            $create = Renting::create($post);
            //吧新注册的用户id添加到查出来的上级用户的下级用户中
            array_push($users['place_junior'], $create->id);
            //吧下级数组根据，转换为字符串
            $users['place_junior'] = implode(',', $users['place_junior']);
            $newjunior = config('public.newjunior');
            $users['place_price'] = $users['place_price'] + $newjunior;
            $users['place_prices'] = $users['place_prices'] + $newjunior;
            //修改上级用户数据
            $sql = Renting::where('id', $users['id'])->update($users);
            if ($create && $sql) {
                \DB::commit();
                return ['code' => 200, 'msg' => '注册成功', 'data' => []];
            }
        } catch (\Exception $exception) {
            \DB::rollBack();
            return ['code' => 500, 'msg' => '注册失败', 'data' => []];
        }
    }

    /**
     * @param $phone
     * @return mixed
     * User: 张浩
     * Date: 2022/11/11
     * Time: 14:13
     * 根据手机号获取一条数据
     */
    public static function phoneRentingfirst($phone)
    {
        return Renting::where('phone', $phone)->first()->toArray();
    }

    /**
     * @param $post
     * @return array
     * User: 张浩
     * Date: 2022/11/11
     * Time: 14:10
     * 用户正常注册
     */
    public static function wxEnrollLogin($post)
    {
        $post['nickName'] = '手机用户' . time();
        $post['password'] = md5($post['password']);
        $create = Renting::create($post);
        if ($create) return CommonService::success(200, '注册成功', $create);
        return CommonService::fail(9999, '注册失败', []);
    }

    /**
     * @param $post
     * @return array
     * User: 张浩
     * Date: 2022/11/11
     * Time: 8:27
     * 微信授权登录
     */
    public static function wxLogin($post)
    {
        //根据openid获取用户信息
        $user = Renting::where('openid', $post['openid'])->first();
        //判断是否获取成功
        if (empty($user)) {
            //无数据时添加用户信息并生成token
            $date = Renting::create($post);
            $token = CommonService::createToken($date->id);
            Cache::put('qiweitoken', $token);
            $data=[
                'token'=>$token,
                'uid'=>$date->id
            ];
            return CommonService::success(200, '登录成功', $data);
        } else {
            //有数据是修改信息并生成token
            $date = Renting::where('openid', $post['openid'])->update($post);
            if ($date) {
                $token = CommonService::createToken($user['id']);
                Cache::put('qiweitoken', $token);
                $data=[
                    'token'=>$token,
                    'uid'=>$user['id']
                ];
                return CommonService::success(200, '登录成功', $data);
            } else {
                return CommonService::fail(9999, '登录失败', []);
            }
        }
    }
}
